package com.dongdong.service.impl;

import com.dongdong.em.Result;
import com.dongdong.em.ResultEnum;
import com.dongdong.mapper.TbUserMapper;
import com.dongdong.model.TbUser;
import com.dongdong.model.TbUserExample;
import com.dongdong.service.UserService;
import com.dongdong.util.ShiroUtils;
import com.dongdong.vo.UserVO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.mail.internet.MimeMessage;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper userMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired(required = false)//required=false表示当前Bean如果有的话直接注入,没有的话忽略跳过
    private JavaMailSender javaMailSender;//https://blog.csdn.net/Falcon__/article/details/128909127

    @Override
    public TbUser getUserByName(String username) {
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<TbUser> tbUsers = userMapper.selectByExample(example);
        if (tbUsers != null && tbUsers.size() > 0) {
            return tbUsers.get(0);
        }
        return null;
    }


    @Override
    public String generateCode(@RequestParam String phone) {
        if (phone == null && phone == "" && "".equals(phone)) {
            return "error";
        }
        rocketMQTemplate.convertAndSend("generateCode", phone);
        return "ok";
    }

    @Override
    public int register(TbUser user) {//注册需要发送消息 MQ 将手机号 生成验证码
        String salt = ShiroUtils.generateSalt(5);
        String md5 = ShiroUtils.encryptPassword("md5", user.getPassword(), salt, 5);/*加密次数 和 解密次数成正比*/

        //判断name是否重复
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(user.getUsername());
        List<TbUser> tbUsers = userMapper.selectByExample(example);
        if (tbUsers.size() != 0) {
            return 999;//有数据说明有重复的sellerid
        }

        //不重复就增加密码和盐
        user.setPassword(md5);
        user.setSalt(salt);
        user.setCreated(new Date());
        user.setUpdated(new Date());
        user.setStatus("Y");

        //增加 - 注册
        int i = userMapper.insertSelective(user);
        return i;
    }


    @Override
    public TbUser getUserName(String username) {
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(username);

        List<TbUser> tbUsers = userMapper.selectByExample(example);

        if (tbUsers != null && tbUsers.size() > 0) {
            return tbUsers.get(0);
        }
        return null;
    }

    @Override
    public int updatePwdSatl(String username, String md5, String salt) {
        //1.根据用户名查询出来数据  然后进行修改
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(username);

        List<TbUser> tbUsers = userMapper.selectByExample(example);

        for (TbUser tbUser : tbUsers) {
            tbUser.setPassword(md5);
            tbUser.setSalt(salt);

            int i = userMapper.updateByPrimaryKeySelective(tbUser);

            return i;
        }
        return 0;
    }

    @Override
    public Result getUserByBirthday() {
        //1.查询出所有的生日期
        List<TbUser> tbBirthday = userMapper.getUserByBirthday();
        //遍历 判断 发送次数
        for (TbUser user : tbBirthday) {
            Date birthday = user.getBirthday();
            String email = user.getEmail();
            String username = user.getUsername();

            //2.判断用户生日是否是当前日期  如果是的话  就发送日期   如果不是 就提示还没到生日
            //用户生日日期格式转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String userbirthday = sdf.format(birthday);

            //当前日期格式转换
            Date now1 = new Date();
            String nowDate1 = sdf.format(now1);

            if (!userbirthday.equals(nowDate1)) {//判断是否到生日
                continue;
            }

            //3.判断用户email是否为空  如果为空就不发送
            if (email.isEmpty()) {
                return Result.ZHRESPONSE(ResultEnum.EMAIL_IS_NULL);//email 为空 无法发送
            }

            //4.编写发送的内容  使用javaemail 对用户发送邮件
            //创建发送邮件对象
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            //发件人邮箱
            simpleMailMessage.setFrom("3287618159@qq.com");
            //定义一个数组 将发件人和收件人放进去
            String[] str = {email};/*从数据库查询到的email根据获取到的email进行发送邮件*/
            simpleMailMessage.setTo(str);
            //设置邮件主题目
            simpleMailMessage.setSubject("XXX公司-给您的生日祝福");//标题
            simpleMailMessage.setText("亲爱的:" + username + "," + userbirthday + "今天是你的生日,祝你生日快乐!");//内容  这里是hutool生成的随机密码
            //设置发送时间
            simpleMailMessage.setSentDate(new Date());
            //执行发送
            javaMailSender.send(simpleMailMessage);
        }
        return Result.ZHRESPONSE(ResultEnum.SEND_BIRTHDAY_SUCCESS);
    }
}
